﻿//-----------------------------------------------------------------------
// <copyright file="Book.cs" company="FIT VUT">
//     Copyright (c) FIT VUT. All rights are reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
using System.Windows.Shapes;
using BookCaching.Data.Storage;
using BookCaching.Localization;

namespace BookCaching.Data
{
    public class Book : IIsolatedStorage<Book>
    {
        #region Constructors
        public Book()
        {
        }

        public Book(Knihotoc_WCFService.Book wcfBook)
        {
            this.convertFromAzureServiceBook(wcfBook);
        }

        //public Book(int id, string title, string author, string coverImagePath)
        //{
        //    if ((title != null) && (title.Length > 0))
        //    {
        //        this.Title = title;
        //        this.TitleFirstLetter = new string(title[0], 1).ToUpper();
        //    }

        //    if ((author != null) && (author.Length > 0))
        //    {
        //        this.Author = author;
        //        this.AuthorFirstLetter = new string(author[0], 1).ToUpper();
        //    }

        //    this.ID = id;

        //    if ((coverImagePath != null) && (coverImagePath.Length > 0))
        //    {
        //        this.CoverImagePath = coverImagePath;
        //    }

        //    this.ImageThumbnailVisibility = Visibility.Visible;

        //    this.SaveToIsolatedStorage();
        //}

        //public Book(int id, string title, string author, string coverImagePath, string ISBN, int? year, float? lat, float? lon)
        //{
        //    if ((title != null) && (title.Length > 0))
        //    {
        //        this.Title = title;
        //        this.TitleFirstLetter = new string(title[0], 1).ToUpper();
        //    }

        //    if ((author != null) && (author.Length > 0))
        //    {
        //        this.Author = author;
        //        this.AuthorFirstLetter = new string(author[0], 1).ToUpper();
        //    }

        //    this.ID = id;

        //    if ((coverImagePath != null) && (coverImagePath.Length > 0))
        //    {
        //        this.CoverImagePath = coverImagePath;
        //    }

        //    this.ImageThumbnailVisibility = Visibility.Visible;

        //    this.ISBN = ISBN;
        //    this.YearPublished = (year == null? "" : ""+year);
        //    this.Lat = lat;
        //    this.Lon = lon;
        //    this.SaveToIsolatedStorage();
        //}
        #endregion

        #region Public properties
        /// <summary>
        /// Gets or sets the book's ID.
        /// </summary>
        public int ID { get; set; }
        
        /// <summary>
        /// Gets or sets the reference to the book's owner.
        /// </summary>
        public int OwnerRef { get; set; }
        
        /// <summary>
        /// Gets or sets the reference to the current reader of the book.
        /// </summary>
        public int? CurrentUserRef { get; set; }
        
        /// <summary>
        /// Gets or sets the ISBN of the book.
        /// </summary>
        public string ISBN { get; set; }
        
        /// <summary>
        /// Gets or sets the book's title.
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// Gets the first letter of the book's title (for grouping purposes).
        /// </summary>
        public string TitleFirstLetter { get; private set; }
        
        /// <summary>
        /// Gets or sets the author's name.
        /// </summary>
        public Author Author { get; set; }
        
        /// <summary>
        /// Gets or sets the first letter of the author's name (for grouping purposes).
        /// </summary>
        public string AuthorFirstLetter { get; set; }
        
        /// <summary>
        /// Gets or sets the year when the book was published.
        /// </summary>
        public string YearPublished { get; set; }
        
        /// <summary>
        /// Gets or sets address reference.
        /// </summary>
        public Address Address { get; set; }
        
        /// <summary>
        /// Gets or sets the internal state of the book (determines if the book is rented etc).
        /// </summary>
        public int BookState { get; set; }
        
        /// <summary>
        /// Gets or sets the path to the cover image.
        /// </summary>
        public string CoverImagePath { get; set; }

        /// <summary>
        /// Gets or sets the visibility of the book's thumbnail image.
        /// </summary>
        public Visibility ImageThumbnailVisibility { get; set; }
        #endregion

        public void convertFromAzureServiceBook(Knihotoc_WCFService.Book wcfBook)
        {
            Author = new Author(wcfBook.Author);
            AuthorFirstLetter = new string(this.Author.LastName[0], 1).ToUpper();
            BookState = wcfBook.BookState;
            CoverImagePath = wcfBook.ImageLink;
            CurrentUserRef = wcfBook.CurrentUserRef;
            ID = wcfBook.ID;
            ISBN = wcfBook.ISBN;
            if (wcfBook.Address != null)
            {
                Address = new Address(wcfBook.Address);
            }
            OwnerRef = wcfBook.OwnerRef;            
            Title = wcfBook.Title;
            TitleFirstLetter = new string(this.Title[0], 1).ToUpper();
            YearPublished = wcfBook.YearPublished.ToString();
        }

        public bool SaveToIsolatedStorage()
        {
            bool everythingAdded = true;

            if (this.Title != null)
            {
                if (!IsolatedStorageHandler.Instance.AddOrUpdateValue("Book_" + this.ID.ToString() + "_Title", this.Title))
                {
                    everythingAdded = false;
                }
            }
            else
            {
                return false;
            }

            if (this.Author != null)
            {
                if (!IsolatedStorageHandler.Instance.AddOrUpdateValue("Book_" + this.ID.ToString() + "_Author", this.Author))
                {
                    everythingAdded = false;                         
                }
            }
            else
            {
                everythingAdded = false;
            }

            if (this.CoverImagePath != null)
            {
                if (!IsolatedStorageHandler.Instance.AddOrUpdateValue("Book_" + this.ID.ToString() + "_CoverImagePath", this.CoverImagePath))
                {
                    everythingAdded = false;
                }
            }
            else
            {
                everythingAdded = false;
            }

            if (everythingAdded)
            {
                return true;
            }
            else
            {
                IsolatedStorageHandler.Instance.RemoveValue("Book_" + this.ID.ToString() + "_Title");
                IsolatedStorageHandler.Instance.RemoveValue("Book_" + this.ID.ToString() + "_Author");
                IsolatedStorageHandler.Instance.RemoveValue("Book_" + this.ID.ToString() + "_CoverImagePath");

                return false;
            }
        }

        public void LoadFromIsolatedStorage(string id)
        {
            this.Title = IsolatedStorageHandler.Instance.GetValueOrDefault<string>(
                "Book_" + id + "_Title", LocalizedStrings.Get("BookNotFound"));
            
            if ((this.Title != null) && (this.Title.Length > 0))
            {
                this.TitleFirstLetter = new string(this.Title[0], 1).ToUpper();
            }

            //this.Author = IsolatedStorageHandler.Instance.GetValueOrDefault<string>(
            //    "Book_" + id + "_Author", LocalizedStrings.Get("AuthorNotFound"));
            
            //if ((this.Author != null) && (this.Author.Length > 0))
            //{
            //    this.AuthorFirstLetter = new string(this.Author[0], 1).ToUpper();
            //}

            this.CoverImagePath = IsolatedStorageHandler.Instance.GetValueOrDefault<string>(
                "Book_" + id + "_CoverImagePath", "/Images/NoImage.jpg");

            this.ISBN = IsolatedStorageHandler.Instance.GetValueOrDefault<string>(
                "Book_" + id + "_ISBN", LocalizedStrings.Get("IsbnNotFound"));

            this.YearPublished = IsolatedStorageHandler.Instance.GetValueOrDefault<string>(
                "Book_" + id + "_YearPublished", LocalizedStrings.Get("YearPublishedNotFound"));
        }
    }
}
